/*
** my_str_to_wordtab.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Mon Jul  4 15:35:52 2011 michael guidet
** Last update Mon Jul  4 15:35:53 2011 michael guidet
*/

#include "server.h"

int	count_word(char *str, char del)
{
  int	i;
  int	nb_word;

  nb_word = 0;
  for (i = 0; str[i]; i++)
    {
      if (str[i] != del && (str[i + 1] == del || str[i + 1] == '\0'))
	nb_word++;
    }
  return (nb_word);
}

char	**str_to_wordtab(char *str, char del)
{
  char	**tab;
  int	nb_word;
  char	*cpy;
  int	i;
  int	j;

  nb_word = count_word(str, del);
  tab = x_null(NULL, malloc(sizeof(*tab) * (nb_word + 1)), "malloc");
  cpy = x_null(NULL, strdup(str), "strdup");
  for (i = 0; cpy[i]; i++)
    if (cpy[i] == del)
      cpy[i] = '\0';
  j = 0;
  for (i = 0; i < nb_word; i++)
    {
      while (!cpy[j++]);
      tab[i] = &cpy[j - 1];
      while (cpy[j++]);
    }
  tab[i] = 0;
  return (tab);
}
